package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.common.Result;
import com.yqg.education.common.interceptor.SysLog;
import com.yqg.education.entity.Role;
import com.yqg.education.entity.RoleMenu;
import com.yqg.education.entity.UserRole;
import com.yqg.education.service.IRoleMenuService;
import com.yqg.education.service.IRoleService;
import com.yqg.education.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * RoleController 前端控制器
 * @author zxf
 * @since 2025-03-01
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private IRoleService roleService;

    @Autowired
    private IRoleMenuService roleMenuService;

    @Resource
    private IUserRoleService userRoleService;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
//    @SysLog("新增或者修改角色")
    @Transactional(rollbackFor = Exception.class)
    public Result save(@RequestBody Role role) {
        if (role.getId() == null) {
            role.setIsDelete(0);
            roleService.saveOrUpdate(role);
            ArrayList<RoleMenu> roleMenus = new ArrayList<>();
            List<Long> menuIds = role.getMenuIds();
            for (Long menuId : menuIds) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setMenuId(menuId);
                roleMenu.setRoleId(role.getId());
                roleMenus.add(roleMenu);
            }
            return Result.success(roleMenuService.saveBatch(roleMenus));
        } else {
            role.getMenuIds().add(role.getId());
            roleService.saveOrUpdate(role);
            return Result.success(roleMenuService.updateRoleMenu(role.getMenuIds()));
        }
    }
    /**
     * 删除时先进行判断
     */
    @GetMapping("delete/{id}")
//    @SysLog("删除角色")
    public Result delete(@PathVariable Integer id) {
        LambdaQueryWrapper<UserRole> userRoleWrapper = new LambdaQueryWrapper<>();
        userRoleWrapper.eq(UserRole::getRoleId, id);
        List<UserRole> list = userRoleService.list(userRoleWrapper);
        if (list.size() > 0) {
            return Result.error("500", "当前角色下存在用户，禁止删除");
        }
        Role role = roleService.getById(id);
        role.setIsDelete(1);
        return Result.success(roleService.saveOrUpdate(role));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
       List<Role> list=  roleService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(roleService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");
        return Result.success(roleService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

